﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IMLibrary
{
    public sealed class BitWriteRead
    {
 
        /// <summary>
        /// 设置字节中位的值
        /// </summary>
        /// <param name="theByte">字节</param>
        /// <param name="index">要设置的位,值从低到高为1-8</param>
        /// <param name="flag">要设置的值true或false</param>
        /// <returns></returns>
        public static byte  SetBitValue(ref  byte theByte, int index, bool flag)
        {
            index--;
            if (index < 8 && index >= 0)
            {  
                if (flag)
                    theByte |= (byte)(1 << index);        // 将nPos的bit位设置为1，其他位不变  
                else 
                    theByte &= (byte)~(1 << index);       // 将nPos的bit位设置为0，其他位不变    
            }
            return theByte;
        }

        /// <summary>
        /// 获取字节中位的值
        /// </summary>
        /// <param name="theByte">字节</param>
        /// <param name="index">要设置的位,值从低到高为 1-8</param>
        /// <returns></returns>
        public static bool GetBitValue(byte theByte, int index)
        { 
            if (index <= 8 && index >=1)
            {
                //x << 1 = x * 2 
                int i = 2 << (index - 2);
                if (i == 0) i = 1;
                //Console.Write(i.ToString());
                if ((theByte & i) == i)
                    return true;
                else
                    return false;
            }
            return false;
        }

        /// <summary>
        /// 从字节数组获取消息发送者
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte GetCommandType(byte[] data)
        {
            if (data == null || data.Length < BinaryPacket.BinaryMsg.HeaderLength)
                return 0;
            else
                return data[1];
        }
        /// <summary>
        /// 设置命令
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static void SetCommandType( byte[] data, byte Command)
        {
            if (data == null || data.Length < BinaryPacket.BinaryMsg.HeaderLength)
                return;
            else
                data[1] = Command;
        }

        /// <summary>
        /// 从字节数组获取消息发送者
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static UInt32 GetFrom(byte[] data)
        {
            if (data == null || data.Length < BinaryPacket.BinaryMsg.HeaderLength)
                return 0;
            else
                return BitConverter.ToUInt32(data, 2);
        }

        /// <summary>
        /// 从字节数组获取消息接收者
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static UInt32 GetTo(byte[] data)
        {
            if (data == null || data.Length < BinaryPacket.BinaryMsg.HeaderLength)
                return 0;
            else
                return BitConverter.ToUInt32(data, 6);
        }
    }
}
